﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace UtilEditors
{
    public class GUIStyleViewer : EditorWindow
    {
        private Vector2 scrollVector2 = Vector2.zero;
        private string search = "";

        [MenuItem("Tools/辅助/GUIStyle查看器")]
        public static void InitWindow()
        {
            EditorWindow.GetWindow(typeof(GUIStyleViewer));
        }

        void OnGUI()
        {
            GUILayout.BeginHorizontal("HelpBox");
            GUILayout.Space(30);
            search = EditorGUILayout.TextField("", search, "SearchTextField", GUILayout.MaxWidth(position.x / 3));
            GUILayout.Label("", "SearchCancelButtonEmpty");
            GUILayout.EndHorizontal();
            scrollVector2 = GUILayout.BeginScrollView(scrollVector2);
            foreach (GUIStyle style in GUI.skin.customStyles)
            {
                if (style.name.ToLower().Contains(search.ToLower()))
                {
                    DrawStyleItem(style);
                }
            }
            GUILayout.EndScrollView();
        }

        void DrawStyleItem(GUIStyle style)
        {
            GUILayout.BeginHorizontal("box");
            GUILayout.Space(40);
            EditorGUILayout.SelectableLabel(style.name);
            GUILayout.FlexibleSpace();
            EditorGUILayout.SelectableLabel(style.name, style);
            GUILayout.Space(40);
            EditorGUILayout.SelectableLabel("", style, GUILayout.Height(40), GUILayout.Width(40));
            GUILayout.Space(50);
            if (GUILayout.Button("复制"))
            {
                TextEditor textEditor = new TextEditor();
                textEditor.text = "GUI.skin.FindStyle(\"" + style.name + "\")";
                textEditor.OnFocus();
                textEditor.Copy();
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(10);
        }
    }



    public class MyUnityTextureWindow : EditorWindow
    {
        [MenuItem(("Tools/辅助/Unity图标"))]
        static void Init()
        {
            GetWindow<MyUnityTextureWindow>("图标");
        }
        Vector2 m_Scroll;
        List<string> m_Icons = null;
        void Awake()
        {
            m_Icons = new List<string>();
            Texture2D[] t = Resources.FindObjectsOfTypeAll<Texture2D>();
            foreach (Texture2D x in t)
            {
                Debug.unityLogger.logEnabled = false;
                GUIContent gc = EditorGUIUtility.IconContent(x.name);
                Debug.unityLogger.logEnabled = true;
                if (gc != null && gc.image != null)
                {
                    m_Icons.Add(x.name);
                }
            }
            Debug.Log(m_Icons.Count);
        }
        void OnGUI()
        {
            m_Scroll = GUILayout.BeginScrollView(m_Scroll);
            float width = 50f;
            int count = (int)((position.width - 40) / width);
            for (int i = 0; i < m_Icons.Count; i += count)
            {
                GUILayout.BeginHorizontal();
                for (int j = 0; j < count; j++)
                {
                    int index = i + j;
                    if (index < m_Icons.Count)
                    {
                        var icon = m_Icons[index];
                        //EditorGUILayout.SelectableLabel(icon);
                        if (GUILayout.Button(EditorGUIUtility.IconContent(icon), GUILayout.Width(width), GUILayout.Height(30)))
                        {
                            var vs = "EditorGUIUtility.IconContent(\"" + icon + "\")";
                            debug.log(vs);
                            TextEditor textEditor = new TextEditor();
                            textEditor.text = vs;
                            textEditor.OnFocus();
                            textEditor.Copy();
                        }
                    }
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndScrollView();
        }
    }
}